package com.home766.dao.impl;

import com.home766.dao.IDictionaryDao;
import com.home766.dao.OrderRewardDao;
import com.home766.entity.OrderReward;
import net.biz.dao.NativeBaseDao;
import net.biz.dao.ProductCategoryDao;
import net.biz.dao.impl.BaseDaoImpl;
import net.biz.entity.Brand;
import net.biz.util.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门店下单奖励提成点-dao
 */
@Repository("orderRewardDaoImpl")
public class OrderRewardDaoImpl extends BaseDaoImpl<OrderReward, Long> implements OrderRewardDao{
	
	@Resource(name="productCategoryDaoImpl")
	private ProductCategoryDao productCategoryDao;
	@Autowired
	private IDictionaryDao iDictionaryDao;
	
	@Autowired
	private NativeBaseDao nativeBaseDao;
	/**
	 * 根据类型和分类品牌查询
	 * @param type     类型
	 * @param category 分类
	 * @param brand    品牌
	 * @return
	 */
	public OrderReward findByTypeAndCategory(Integer type, Long category, Brand brand) {
		if(type == null || category == null || brand == null){
			return null;
		}
		String sql=" select * from 766_order_reward ore where ore.type=:type and ore.brand=:brand ";
		List<OrderReward> list = null;
		Map<String, Object> params=new HashMap<String,Object>();
		params.put("type", type);
		params.put("brand", brand.getId());
		params.put("category", category);
		if(type == ParamsUtil.CART_TYPE_NORMAL){
			sql += " and ore.product_category = :category";
			list = nativeBaseDao.findList(sql, params, OrderReward.class);
		}else{
			sql += " and ore.scheme_category = :category";
			list = nativeBaseDao.findList(sql, params, OrderReward.class);
		}
		if(list != null){
			return list.size() > 0 ? list.get(0):null;			
		}
		return null;
	}

}
